package development;

import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.Graphics;
import state.Key;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.array.StringArray;
import development.socket.SocketClientUtil;

public class FrmChangePassword extends GameForm {
  private ImageText text8;

  private int chooseIndex;

  private TextField oldPasswordTextField;
  private TextField newPasswordTextField;
  private TextField newPasswordTextField1;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmChangePassword();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);

    oldPasswordTextField = new TextField(_manager, TextField.CONSTRAINT_PASSWORD, Constants.TEXT_FIELD_STYLE);
    newPasswordTextField = new TextField(_manager, TextField.CONSTRAINT_PASSWORD, Constants.TEXT_FIELD_STYLE);
    newPasswordTextField1 = new TextField(_manager, TextField.CONSTRAINT_PASSWORD, Constants.TEXT_FIELD_STYLE);

    if (GameConstants.IS_240x320_SCREEN) {
      oldPasswordTextField.setPosition(75, 95);
      oldPasswordTextField.setSize(150, 20);
      newPasswordTextField.setPosition(75, 125);
      newPasswordTextField.setSize(150, 20);
      newPasswordTextField1.setPosition(75, 165);
      newPasswordTextField1.setSize(150, 20);
    } else {
      oldPasswordTextField.setPosition(128, 74);
      oldPasswordTextField.setSize(150, 20);
      newPasswordTextField.setPosition(128, 103);
      newPasswordTextField.setSize(150, 20);
      newPasswordTextField1.setPosition(128, 142);
      newPasswordTextField1.setSize(150, 20);
    }
    
    DrawUtils.setBottomBar(_manager, "Trở về", "Xác nhận", "Xóa");
    chooseIndex = 0;// up first
    updateFocus();
  }

  private void drawFor240x320(Graphics g) {
    text8.drawString2(g, "MK cũ", GameColor.WHITE_CODE, 15, 100, GameConstants.TOP_LEFT_ANCHOR);
    oldPasswordTextField.draw(g);
    text8.drawString2(g, "MK mới", GameColor.WHITE_CODE, 15, 130, GameConstants.TOP_LEFT_ANCHOR);
    newPasswordTextField.draw(g);
    text8.drawString2(g, "Nhập lại", GameColor.WHITE_CODE, 15, 160, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, "mật khẩu", GameColor.WHITE_CODE, 15, 175, GameConstants.TOP_LEFT_ANCHOR);
    newPasswordTextField1.draw(g);
  }

  private void drawFor320x240(Graphics g) {
    text8.drawString2(g, "Mật khẩu cũ", GameColor.WHITE_CODE, 45, 76, GameConstants.TOP_LEFT_ANCHOR);
    oldPasswordTextField.draw(g);
    text8.drawString2(g, "Mật khẩu mới", GameColor.WHITE_CODE, 45, 105, GameConstants.TOP_LEFT_ANCHOR);
    newPasswordTextField.draw(g);
    text8.drawString2(g, "Nhập lại", GameColor.WHITE_CODE, 45, 134, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, "mật khẩu", GameColor.WHITE_CODE, 45, 149, GameConstants.TOP_LEFT_ANCHOR);
    newPasswordTextField1.draw(g);
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);

    if (GameConstants.IS_240x320_SCREEN) {
      drawFor240x320(g);
    } else {
      drawFor320x240(g);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.ENTER:
      changePassword();
      break;
    case Key.UP:
      chooseIndex--;
      if (chooseIndex < 0) {
        chooseIndex = 2;
      }
      updateFocus();
      break;
    case Key.DOWN:
      chooseIndex++;
      if (chooseIndex > 2) {
        chooseIndex = 0;
      }
      updateFocus();
      break;
    case Key.SOFT_LEFT:
      GameGlobal.nextState(FrmGameServiceList.instance(), null);
      break;
    case Key.SOFT_RIGHT:
    case Key.BACKSPACE:
      if (chooseIndex == 0) {
        oldPasswordTextField.deleteLastChar();
      } else if (chooseIndex == 1) {
        newPasswordTextField.deleteLastChar();
      } else if (chooseIndex == 2) {
        newPasswordTextField1.deleteLastChar();
      }
      break;
    default:
      if (chooseIndex == 0) {
        oldPasswordTextField.keyReleased(null, keyCode);
      } else if (chooseIndex == 1) {
        newPasswordTextField.keyReleased(null, keyCode);
      } else if (chooseIndex == 2) {
        newPasswordTextField1.keyReleased(null, keyCode);
      }
      break;
    }
  }

  private void changePassword() {
    String oldPassword = oldPasswordTextField.getText();
    String newPassword = newPasswordTextField.getText();
    String newPassword1 = newPasswordTextField1.getText();

    if (oldPassword.length() == 0) {
      GameGlobal.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn cần nhập vào mật khẩu cũ");
      return;
    }

    if (!newPassword.equals(newPassword1)) {
      GameGlobal.alert.showAlert(this, GameAlert.OK_TYPE, "Mật khẩu nhập lại không đúng");
      return;
    }

    if (newPassword.length() < Constants.PASSWORD_MIN_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1("Mật khẩu phải", "dài hơn " + Constants.PASSWORD_MIN_LEN + " ký tự"));
      return;
    }

    if (newPassword.length() > Constants.PASSWORD_MAX_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1("Mật khẩu phải", "ngắn hơn " + Constants.PASSWORD_MAX_LEN + " ký tự"));
      return;
    }

    SocketClientUtil.changePassword(Global.currentUser.getName(), oldPassword, newPassword);
  }

  public void updateFocus() {
    if (chooseIndex == 0) {
      oldPasswordTextField.setFocused(true);
      newPasswordTextField.setFocused(false);
      newPasswordTextField1.setFocused(false);
    } else if (chooseIndex == 1) {
      oldPasswordTextField.setFocused(false);
      newPasswordTextField.setFocused(true);
      newPasswordTextField1.setFocused(false);
    } else if (chooseIndex == 2) {
      oldPasswordTextField.setFocused(false);
      newPasswordTextField.setFocused(false);
      newPasswordTextField1.setFocused(true);
    }
  }

  public void destroy() {
    text8 = null;
    oldPasswordTextField = null;
    newPasswordTextField = null;
    newPasswordTextField1 = null;
  }
}
